﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;
using BAL;
using Common;
using System.IO;
using System.Text;
namespace Web
{
    public partial class Login : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["regok"] != null)
                {
                    if (Request.QueryString["regok"] == "ok")
                    {
                        Utility.ShowBoxyAlert("注册成功，您现在处于待审核状态，只有审核通过才可登陆，请您耐心等待。", this);
                        return;
                    }
                    else if (Request.QueryString["regok"] == "activ")
                    {
                        Utility.ShowBoxyAlert("激活成功，您现在可以登陆录入试题了", this);
                        return;
                    }
                }
                this.IdCard.Attributes.Add("onblur", "CheckIdCard()");//验证身份证的位数
                this.Password.Attributes.Add("onblur", "CheckPassword()");//验证密码的位数
            }
            this.IdCard.Attributes.Add("onblur","CheckIdCard()");//验证身份证的位数
            this.Password.Attributes.Add("onblur","CheckPassword()");//验证密码的位数
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string idcard = null;
            string password = null;
            if (UserAgreement.Checked == false)
            {
                Utility.ShowBoxyAlert("您必须同意用户协议才可继续登录", this);
                AuthCode1.ClearSession();
                return;
            }
            if (!AuthCode1.IsMatch)
            {
                Utility.ShowBoxyAlert("验证码不正确", this);
                AuthCode1.ClearSession();
                return;
            }
            if (IdCard.Text.Trim()==null||string.IsNullOrEmpty(IdCard.Text.Trim()))
            {
                Utility.ShowBoxyAlert("身份证号不允许为空",this);
                AuthCode1.ClearSession();
                return;
            }
            if (IdCard.Text.Trim().Length != 15 && IdCard.Text.Trim().Length != 18)
            {
                Utility.ShowBoxyAlert("身份证输入不正确", this);
                AuthCode1.ClearSession();
                return;
            }
            
            if (!PageValidate.IsNumber(IdCard.Text.Trim()))
            {
                Utility.ShowBoxyAlert("身份证输入不正确", this);
                AuthCode1.ClearSession();
                return;
            }
            else
            {
                idcard = IdCard.Text.Trim();
            }
            if (Password.Text.Trim().Length < 6)
            {
                Utility.ShowBoxyAlert("密码填写不正确", this);
                AuthCode1.ClearSession();
                return;
            }
            else
            {
                password = Password.Text.Trim();
            }
            password = Utility.MD5(password);
            UserLinkExtendInfo uinfo = new UserLinkExtendInfo();
            if (!UserLinkExtend.LoginUser(idcard, password, ref uinfo))
            {
                Utility.ShowBoxyAlert("登录失败，请检查身份证和密码是否错误", this);
                AuthCode1.ClearSession();
                //Response.End();
                return;
            }
            else
            {
                if (uinfo.Status == 1)
                {
                    Utility.ShowBoxyAlert("您所登录的账号还没有通过审核，请耐心等待", this);
                    AuthCode1.ClearSession();
                    //Response.End();
                    return;
                }
                else if (uinfo.Status == 3)
                {
                    Utility.ShowBoxyAlert("您登陆的账号还没有激活，请先激活", this);
                    AuthCode1.ClearSession();
                    //Response.End();
                    return;
                }
                else
                {
                    RoleInfo rinfo = Role.GetRoleInfo(uinfo.RoleId);
                    if (rinfo.Name.StartsWith("教师"))
                    {
                        HttpCookie cookies = new HttpCookie("Teacherxxx");
                        //通过身份证号查询对象
                        UserInfo User_Info = Users.GetUserInfoById(uinfo.Id);
                        Session["userid"] = uinfo.Id;//将教师的编号放入session中
                        cookies.Values["UserId"] = uinfo.Id.ToString();
                        string tCookiesKey = string.Join(",,,", new string[] { idcard, password });
                        User_Info.LoginKey = Guid.NewGuid();
                        User_Info.KeyOutDate = DateTime.Now.AddMinutes(180);
                        cookies.Values["LoginKey"] = DESEncrypt.Encrypt(tCookiesKey, User_Info.LoginKey.ToString());
                        //cookies.Values["email"] = email;
                        cookies.Expires = DateTime.Now.AddMinutes(180);
                        HttpContext.Current.Response.AppendCookie(cookies);
                        Users.Edit(User_Info);
                        Response.Redirect("MyQuestion.aspx");
                        Response.End();
                        return;
                    }
                    else
                    {
                        HttpCookie cookies = new HttpCookie("xxadmin");
                        UserInfo User_Info = Users.GetUserInfoById(uinfo.Id);
                        cookies.Values["UserId"] = uinfo.Id.ToString();
                        string tCookiesKey = string.Join(",,,", new string[] { idcard, password });
                        User_Info.LoginKey = Guid.NewGuid();//密钥
                        User_Info.KeyOutDate = DateTime.Now.AddMinutes(180);//产生密钥时间
                        cookies.Values["LoginKey"] = DESEncrypt.Encrypt(tCookiesKey, User_Info.LoginKey.ToString());
                        cookies.Expires = DateTime.Now.AddMinutes(180);
                        HttpContext.Current.Response.AppendCookie(cookies);
                        Users.Edit(User_Info);
                        //string userid =cookies["UserId"].ToString();
                       // Response.Write("<script>alert('"+userid+"');</script>");
                        Response.Redirect("adminquestion.aspx");
                        Response.End();
                        return;
                    }
                }
            }
        }
    }
}
